<template>
	<view class="content-layout">
		<pai-pay-platform ref="wechatPlatform" :btnBg="'#4DCB6A'" @payInfo="payInfo"></pai-pay-platform>
		<text class="alipay-hint">由于微信安全限制，支付成功后，需要手动点击返回！</text>
	</view>
</template>

<script>
	/**
	 * 微信支付页面
	 * */
	import {
		paiPayPlatform
	} from '../pai-pay-platform/pai-pay-platform.vue'

	// import encode from '../../tools/encode.js'

	export default {
		components: {
			paiPayPlatform
		},
		data() {
			return {
				info: {},
				text: "1"
			}
		},
		methods: {
			initData(option) {
				console.log("-->option2", option)
				setTimeout(() => {
					if (!option || !option.tk || !option.key) {
						option = uni.getStorageSync("query")
					}
					console.log("-->option3", option)
					this.getUrlQuery(option);
					this.$refs.wechatPlatform.initData(this.info.key)
				}, 1000);
			},
			getUrlQuery(params) {
				if (!params) {
					return
				}
				uni.setStorageSync("query", {})
				// let token = params.st ? encode.decodeToken(params.st) : ""
				uni.setStorageSync('basePath', params.path)
				uni.setStorageSync('token', "Bearer" + " " + params.tk)
				this.info.key = params.key;
			},
			payInfo(payInfo) {
				let payData = {
					timeStamp: payInfo.timeStamp, // 时间戳（单位：秒）
					nonceStr: payInfo.nonceStr, // 随机字符串
					package: payInfo.packagestring || payInfo.package, // 固定值
					signType: payInfo.signType, //固定值
					paySign: payInfo.paySign, //签名
				}
				uni.requestPayment({
					provider: "wxpay",
					...payData,
					success: (res) => {
						console.log("success1:", res.errMsg)
						if (res.errMsg == 'requestPayment:ok') {
							this.$refs.wechatPlatform.paySuccessCallback(true)
						} else {
							this.$refs.wechatPlatform.paySuccessCallback(false)
						}
					},
					fail: (err) => {
						this.$refs.wechatPlatform.paySuccessCallback(false)
						console.log('fail:' + JSON.stringify(err));
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		width: 100%;
		background-color: #F7F7F7;
	}

	.content-layout {
		display: flex;
		flex-direction: column;
		flex: 1;
	}

	.alipay-hint {
		height: 60rpx;
		width: 100%;
		text-align: center;
		padding: 16rpx;
		color: rgba(0, 0, 0, 0.65);
		font-size: 24rpx;
	}

	.style-tv {
		width: 70%;
		display: flex;
		justify-content: center;
	}
</style>